package com.css.fxfzaqbz.modules.assess1.repository.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess1.repository.LittleRepotrRepository;
import com.css.fxfzaqbz.modules.assess1.entity.LittleReport;
import com.css.fxfzaqbz.modules.assess1.vo.LittleReportVO;
import com.css.fxfzaqbz.modules.assess1.vo.SubmitLittleRepoetVO;
import com.css.fxfzaqbz.util.PlatformDateUtils;
import com.css.fxfzaqbz.util.PlatformObjectUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author y1fan
 * @description
 */
@Repository
public class LittleRepotrRepositoryImpl implements LittleRepotrRepository {


    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public void save(LittleReport littleReport) {
        String sql="insert into sys_little_report (id,name,status,attach_id,create_user,craete_time,del_flag," +
                "ia_id,number) " +
                "values (:id,:name,:status,:attachId,:createUser,:craeteTime,:delFlag," +
                ":iaId,:number)";
        BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(littleReport);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public List<LittleReport> getAll(LittleReportVO littleReportVO) {
        String sql=null;
        if (PlatformObjectUtils.isEmpty(littleReportVO.getStatus())){
            sql="select * from sys_little_report where ia_id='"+littleReportVO.getIaId()+"' and number='"+littleReportVO.getNumber()+"' and del_flag='"+ YNEnum.N.toString() +"'";
        }else {
            sql="select * from sys_little_report where ia_id='"+littleReportVO.getIaId()+"' and number='"+littleReportVO.getNumber()+"' and del_flag='"+ YNEnum.N.toString() +"' and status='"+YNEnum.Y.toString()+"'";
        }
        List<LittleReport> littleReports = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(LittleReport.class));
        return littleReports;
    }

    @Override
    public void submit(SubmitLittleRepoetVO submitLittleRepoetVO) {
        Timestamp timestamp = PlatformDateUtils.getCurrentTimestamp();
        String sql = "update sys_little_report set submit_time='"+timestamp+"',status = '" + YNEnum.Y + "' , description='"+submitLittleRepoetVO.getDescription()+"' ,update_time = '" + timestamp + "' " + " where id='"+submitLittleRepoetVO.getId()+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public List<LittleReport> getLittleReport(String iaId) {
       String sql="select * from sys_little_report where ia_id='"+iaId+"'  and del_flag='"+ YNEnum.N.toString() +"'";
        List<LittleReport> littleReports = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(LittleReport.class));
        return littleReports;
    }

    @Override
    public void delete(String id) {
        Timestamp timestamp = PlatformDateUtils.getCurrentTimestamp();
        String sql = "update sys_little_report set del_flag = '" + YNEnum.Y + "'  ,update_time = '" + timestamp + "' " + " where id='"+id+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public boolean check(String iaId, String number) {
        try {
            StringBuilder sql = new StringBuilder("select * from sys_little_report where ia_id='" + iaId + "' and del_flag ='" + YNEnum.N.toString() + "' and status='"+YNEnum.Y.toString()+"' and number ='"+number+"'");
            LittleReport littleReport = jdbcTemplate.queryForObject(sql.toString(), new BeanPropertyRowMapper<>(LittleReport.class));
        } catch (DataAccessException e) {
            return true;
        }
        return false;
    }

    @Override
    public void back(String id) {
        Timestamp timestamp = PlatformDateUtils.getCurrentTimestamp();
        String sql = "update sys_little_report set status = '" + 2 + "'  ,update_time = '" + timestamp + "' " + ",description = null  where id='"+id+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public LittleReport getById(String id) {
        try {
            String sql = "select *  from sys_little_report where id='" + id + "' and del_flag ='" + YNEnum.N.toString() + "'";
            LittleReport entity = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(LittleReport.class));
            return entity;
        } catch (
                EmptyResultDataAccessException e) {
            return null;
        }
    }
}
